<?php

if (!defined('APP')) {
    die('error');
}

class Sql extends ApiBase
{

    //构造函数
    public function __construct()
    {
        parent::__construct();
        $this->getDb();
    }

    /*

    //  http://127.0.0.1/wy.audiovdieo.xyz/xiangmu/yijiaxi/wy/api.php?c=Sql&f=u&t=wy_user&cs=openid&vs=21&w=true&kv=openid=2

    //  https://wy.audiovdieo.xyz/xiangmu/yijiaxi/wy/api.php?c=Sql&f=s&t=user&cs=openid&vs=21&w=true&kv=openid=2&l=1000&o=0

     */
    //admin 操作
    public function a()
    {
        $arr = ['q'];
        foreach ($arr as $v) {
            $$v = $_REQUEST[$v];
        }
        //echo $q;
        $r = $this->db->exec($q);
        $this->display($r);
    }

    //万能查询
    public function e()
    {
        $q = $_REQUEST["q"];
        //$this->dump($q);
        // 检测是否包含前缀
        $i = strpos($q, QZ);
        if ($i == -1) {
            die('table qian zhui error');
        }
        $r = $this->db->exec($q);
        $this->display($r);
    }

    //选择
    public function s()
    {
        $arr = ["t", "cs", "jo", "w", "ob", "l", "o"];
        $q = "select ";

        foreach ($arr as $k => $v) {
            $$v = isset($_REQUEST[$v]) ? $_REQUEST[$v] : " ";
        }

        if ($cs == " ") {
            $cs = " * ";
        }
        $q .= " $cs ";

        if ($t == " ") {
            die("table undefined ");
        }
        $q .= " from " . QZ . "$t ";

        if ($jo == " ") {
            $jo = "  ";
        }
        $q .= " " . $jo . " ";

        if ($w == " ") {
            $w = " true ";
        }
        $q .= " where $w ";

        if ($ob == " ") {
            $ob = " id desc ";
        }
        $q .= " order by $ob ";

        if ($l == " ") {
            $l = " 20 ";
        }
        $q .= " limit $l ";

        if ($o == " ") {
            $o = " 0 ";
        }
        $q .= " offset $o ";

        $r = $this->db->exec($q);
        $this->display($r);

    }

    //输入
    public function i()
    {
        $arr = ["t", "cs", "vs"];
        $q = 'insert into ';
        foreach ($arr as $k => $v) {
            $$v = isset($_REQUEST[$v]) ? $_REQUEST[$v] : " ";
        }

        if ($t == " ") {
            die("table undefined ");
        }
        $q .= QZ . "$t ";
        $q .= " ( $cs ) values ( $vs )";

        $r = $this->db->i_n($q);
        $this->display($r);
    }

    //删除
    public function d()
    {
        $arr = ["t", "w", "l"];
        $q = "delete from ";
        foreach ($arr as $k => $v) {
            $$v = isset($_REQUEST[$v]) ? $_REQUEST[$v] : " ";
        }

        if ($t == " ") {
            die("table undefined ");
        }
        $q .= $t;

        if ($w != " ") {
            $q .= " where $w ";
        }
        if ($l != " ") {
            $q .= " limit $l ";
        }
        echo $this->db->d($q);
        /*
    $r=$this->db->exec($q);
    $this->display($r);
     */
    }

    //更新
    public function u()
    {
        $arr = ["t", "kv", "w", "l"];
        $q = "update ";
        foreach ($arr as $k => $v) {
            $$v = isset($_REQUEST[$v]) ? $_REQUEST[$v] : " ";
        }

        if ($t == " ") {
            die("table undefined ");
        }
        $q .= QZ . "$t ";

        if ($kv == " ") {
            die("update mast have keyValue ");
        }
        $q .= " set  $kv ";
        if ($w == " ") {
            $w = " true ";
        }
        $q .= " where $w ";

        if ($l != " ") {
            $q .= $l;
        }
        echo $this->db->u($q);
        //$this->dump($q);

    }

    //输出
    public function display($r)
    {
        if (is_object($r)) {
            /*
            echo '<br>current_field '. $r->current_field;
            echo '<br>field_count '.$r->field_count;
            echo '<br>lengths '.$r->lengths;
            echo '<br>num_rows '.$r->num_rows;
            echo '<br>type '.$r->type;
             */
            $outputArray = [];
            while ($obj = $this->db->result_getobj($r)) {
                $outputArray[] = $obj;
            }
            echo json_encode($outputArray);
        } else

        if (is_bool($r)) {
            echo //'bool:', $r,
            json_encode($r);
        } else

        if (is_string($r)) {
            echo //'string:', $r;
            json_encode($r);
        } else

        if (is_numeric($r)) {
            echo //'number:',$r;
            json_encode($r);
        } else {
            $this->dump($r);
        }
    }

    //web输出
    private function dump($r)
    {
        echo '<pre>';
        var_dump($r);
        echo '</pre>';
    }

}
